473,424 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,424 software developers and data experts.

Question on MouseUp/MouseDown events with form buttons

Hi again, thanks everyone for your previous help. But having resolved
past problems, I'm moving on to new problems :(

This one is a simple winforms application with two buttons, named
Button1 and Button2, on it (code below). When I press the mouse button
over Button2, and don't release it, in the IDE output window it prints
"Button2_MouseDown" only. If I release the mouse button over the
button, over the form, or even over ANY other application that is
running I also see "Button2_MouseUp" in the output window. This is
what I want and expected.

But when I do exactly the same for Button1, I don't see this. I press
button1, don't release the mouse button, a yellow form is displayed,
and what I am wanting is that when I release the mousebutton the
yellow form is hidden/disappear. Depending on where I release the
mouse button seems to change whether the "MouseUp" event is fired and
therefore whether I hide my yellow form. Why? For example, press the
left mouse button while over Button1, and keep the mousebutton
depressed, a yellow backcolor form will be displayed, now move the
cursor somewhere (over the desktop) still holding down the mouse
button, release the mouse button, and the "MouseUp" event isn't fired!
Ha? I want it to be fired so I can hide the yellow form. So there has
to be a trick - but what is it.

Thank you again
Colin

Public Class Form1
Inherits System.Windows.Forms.Form

//and include the Windows Form Designer Code in here

Dim WithEvents f As Form

Private Sub Button1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseDown
Debug.WriteLine("Button1_MouseDown")
f = New Form
f.Size = New Size(80, 80)
f.BackColor = Color.Yellow
f.Show()
End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseUp
Debug.WriteLine("Button1_MouseUp")
f.Close()
f.Hide()
f = Nothing
End Sub

Private Sub Button2_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button2.MouseDown
Debug.WriteLine("Button2_MouseDown")
End Sub

Private Sub Button2_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button2.MouseUp
Debug.WriteLine("Button2_MouseUp")
End Sub
End Class
Nov 20 '05 #1
4 4329
"Colin McGuire" <co***********@lycos.co.uk> schrieb
Hi again, thanks everyone for your previous help. But having
resolved past problems, I'm moving on to new problems :(

This one is a simple winforms application with two buttons, named
Button1 and Button2, on it (code below). When I press the mouse
button over Button2, and don't release it, in the IDE output window
it prints "Button2_MouseDown" only. If I release the mouse button
over the button, over the form, or even over ANY other application
that is running I also see "Button2_MouseUp" in the output window.
This is what I want and expected.

But when I do exactly the same for Button1, I don't see this. I
press button1, don't release the mouse button, a yellow form is
displayed, and what I am wanting is that when I release the
mousebutton the yellow form is hidden/disappear. Depending on where I
release the mouse button seems to change whether the "MouseUp" event
is fired and therefore whether I hide my yellow form. Why? For
example, press the left mouse button while over Button1, and keep the
mousebutton depressed, a yellow backcolor form will be displayed, now
move the cursor somewhere (over the desktop) still holding down the
mouse button, release the mouse button, and the "MouseUp" event isn't
fired! Ha? I want it to be fired so I can hide the yellow form. So
there has to be a trick - but what is it.


Whenever you press the mouse button, the control "captures" the mouse, so
all mouse messages will be sent to the button even if the cursor is outside
the button. As soon as you show the Form, the mouse is released. Untested
suggestion: Set Button1.Capture = True after showing the Form.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
Thank you Armin - that works a treat, however because I didn't explain
things properly, it isn't what I am after :( My error.

Here is a second attempt at explaining what I am after, much more
specific that the general question I was asking previously. Basically
I want a glorified popup/context menu on a button that shows only when
the mouse button is pressed over the button, and disappears when the
mouse button is released anywhere. Here's a full description.

When the mouse button is depressed over Button1, I want the
popup form (an instance of formPopup) to show. If the mouse
button is released anywhere, the cursor could be anywhere,
even over another application or on the desktop or somewhere else,
I want the popup form to be hidden. The code I have means that
if I move the cursor away from the display form, then release
the mouse button, the form isn't always hidden. I also want
to trap various events in the instance of formPopup, such as
mouseenter etc for controls on that form. By adding
Button1.Capture=True, no events in the instance of formPopup
can be caught.

I hope this is more clear. Thanks again for your previous help & hope
you/others can help me out here.
Colin
Here is the full code I already have.

1. Launch Visual Studio .Net 2003 and create a new winforms
application.
2. Use the VS IDE toolbar to put a new button, Button1, on the form.
3. Paste in the following code

Public Class Form1
Inherits System.Windows.Forms.Form

//and include the Windows Form Designer Code in here

Public Class formPopup
Inherits System.Windows.Forms.Form

Private Sub formPopup_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim b1 As New Button
b1.Size = New Size(15, 15)
b1.Location = New Point(10, 10)
b1.BackColor = Color.Gray
Me.Controls.Add(b1)
AddHandler b1.MouseEnter, AddressOf subMEnter
AddHandler b1.MouseLeave, AddressOf subMLeave
AddHandler b1.MouseUp, AddressOf subMUp

Dim b2 As New Button
b2.Size = New Size(15, 15)
b2.Location = New Point(30, 10)
b2.BackColor = Color.Gray
Me.Controls.Add(b2)
AddHandler b2.MouseEnter, AddressOf subMEnter
AddHandler b2.MouseLeave, AddressOf subMLeave
End Sub

Private Sub subMUp(ByVal sender As Object, _
ByVal e As MouseEventArgs)
Debug.WriteLine("MouseUp")
End Sub

Private Sub subMLeave(ByVal sender As Object, _
ByVal e As EventArgs)
Dim b As Button = CType(sender, Button)
b.BackColor = Color.Gray
End Sub

Private Sub subMEnter(ByVal sender As Object, _
ByVal e As EventArgs)
Dim b As Button = CType(sender, Button)
b.BackColor = Color.Blue
End Sub
End Class
Dim WithEvents f As formPopup

Private Sub Button1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseDown
Debug.WriteLine("Button1_MouseDown")
f = New formPopup

f.Size = New Size(80, 80)
f.BackColor = Color.Yellow

f.Show()
'Button1.Capture = True 'Have to comment this or else I cannot
'trap mouse trap events for controls on the
form
End Sub

Private Sub Button1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseUp, f.MouseUp
Debug.WriteLine("Button1_MouseUp")
f.Close()
f.Hide()
f = Nothing
End Sub
End Class
Nov 20 '05 #3
"Colin McGuire" <co***********@lycos.co.uk> schrieb
Dim WithEvents f As formPopup

Private Sub Button1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseDown
Debug.WriteLine("Button1_MouseDown")
f = New formPopup

f.Size = New Size(80, 80)
f.BackColor = Color.Yellow

f.Show()
'Button1.Capture = True 'Have to comment this or else I cannot
'trap mouse trap events for controls on
the
form
End Sub


Does setting

f.capture = True

help? I tried it and I think that's what you are looking for.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
Thanks Armin but no it doesn't work either (the f.capture = True)
means that the buttons don't capture events (ie change from gray to
blue and back again).

:)

I've learnt a lot trying to do this and have spent the last two nights
trying to get it going. This weekend I will give it another go, try a
different approach.
Thanks again
Colin
"Armin Zingler" <az*******@freenet.de> wrote in message news:<ej**************@TK2MSFTNGP12.phx.gbl>...
"Colin McGuire" <co***********@lycos.co.uk> schrieb
Dim WithEvents f As formPopup

Private Sub Button1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseDown
Debug.WriteLine("Button1_MouseDown")
f = New formPopup

f.Size = New Size(80, 80)
f.BackColor = Color.Yellow

f.Show()
'Button1.Capture = True 'Have to comment this or else I cannot
'trap mouse trap events for controls on
the
form
End Sub


Does setting

f.capture = True

help? I tried it and I think that's what you are looking for.

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Stephen Saunders | last post by:
Hello, I am new to this news group. Please forgive me if this has been asked before. In my VB application, I have next and previous command buttons. I would like to be able to hold the left...
6
by: Dave | last post by:
Hi, With this code, I thought that any 'click' with the mouse would be captured on the window level and nothing would happen, but a click on the button triggers nevertheless the function hit()....
5
by: Tim Bücker | last post by:
Hello. I have a window form with some panels on it. In one panel I am using the OnMouseDown and OnMouseUp events. MouseDown initiates some things and MouseUp releases some things - so it is...
1
by: Nathan Sokalski | last post by:
I want to create a pushbutton-like control on my webform that has an image on it. I used to think that this is what the ImageButton control was, but it seems to me that the ImageButton is nothing...
1
by: Alan | last post by:
i have a form with a label on it Private Sub Label1_mousedown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown ' do stuff End Sub ...
2
by: dave.wayne | last post by:
In a web page I have a div tag that has a onlick event registered through the event listener. However, that same div tag also has a onmousedown - start a drag and drop script The problem I am...
1
by: JDeats | last post by:
It appears the WinForm MouseDown and MouseUp event handlers are not working properly. In the "bare bones" sample application below, Form1_MouseUp gets called even through the mouse button remains...
8
by: Louis | last post by:
I was trying to display a custom context menu in javascript. I wanted to capture a right mouse click. I added an eventListener to a "div" like this.. element,addEventListener ("click", myfunc,...
12
by: Tom Bean | last post by:
I am trying to display a ContextMenuStrip when a user right-clicks on an item in a ListView and have encountered a something that seems strange to me. When the ListView is initially populated,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.